package com.roc.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roc.dao.DaoException;
import com.roc.dao.IBaseDao;
import com.roc.dao.agent.IAgentInfoDao;
import com.roc.dao.impl.BaseDaoImpl;
import com.roc.newPage.IPageDao;
import com.roc.pojo.AuditingItem;
import com.roc.pojo.MessageSend;
import com.roc.pojo.Operators;
import com.roc.pojo.ProfitPercent;
import com.roc.pojo.ProfitRule;
import com.roc.service.ServiceException;
import com.roc.service.agent.IAgentInfoService;
import com.roc.util.Constance;
import com.roc.util.MD5Utils;
import com.roc.util.Utils;
import com.roc.util.constant.OperatorsStatus;
import com.roc.util.constant.SysLogType;

/**
 * @author chenkai
 * @date 2014-08-10
 * 代理商操作
 * */
@Service
@Transactional
public class AgentInfoServiceImpl  implements
		IAgentInfoService {
	static Logger logger=Logger.getLogger(AgentInfoServiceImpl.class);

	@Autowired
	private IAgentInfoDao agentInfoDao;
	
	@Autowired
	private IBaseDao baseDao;

	@Autowired
	private IPageDao pageDao;
	
	@Override
	public Operators queryAgentInfo(String accountNo) throws ServiceException {
		Operators operators=new Operators();
		
		try {
			//查询代理商基本信息
		//	operators=agentInfoDao.queryOperatorInfo(accountNo);
			Object[] params1 = {accountNo};
			operators = (Operators) pageDao.createQueryUniqueResult("from Operators m where m.accountNo = ?", params1);
			
			
			//查询代理商分润规则
		//	List<ProfitRule> profitRuleList=agentInfoDao.queryProfitRule(operators.getAccountNo());
			Object[] params2 = {operators.getAccountNo()};
			List<?> profitRuleLists = (List<?>)pageDao.queryAll("from ProfitRule p where p.agentId = ? order by p.mccTypeCode", params2);
			if(null != profitRuleLists && profitRuleLists.size() > 0){
				List<ProfitRule> profitRuleList = new ArrayList<ProfitRule>();
				for (Object object : profitRuleLists) {
					ProfitRule profitRule = (ProfitRule)object;
					profitRuleList.add(profitRule);
				}
				operators.setProList(profitRuleList);
			}
			//查询分润比例
	//		List<ProfitPercent> profitPerList = agentInfoDao.queryProfitPer(operators.getAccountNo());
			Object[] params3 = {operators.getAccountNo()};
			List<?> profitPerLists = (List<?>)pageDao.queryAll("from ProfitPercent p where p.agent_id = ? order by p.id ", params3);
			if(null != profitPerLists && profitPerLists.size() > 0){
				List<ProfitPercent> profitPerList = new ArrayList<ProfitPercent>();

				for (Object object : profitPerLists) {
					ProfitPercent profitRule = (ProfitPercent)object;
					profitPerList.add(profitRule);
				}
				operators.setProfitPerList(profitPerList);
			}
			
		} catch (DaoException e) {
			logger.debug(e.getMessage(), e);
			throw new ServiceException("获取代理商信息失败");
		}
		
		
		return operators;
	}

	@Override
	public boolean agentAuditingOneInfo(AuditingItem auditing,String accountNo,
			String status) throws ServiceException {
		boolean result=false;
		try {
			agentInfoDao.editSave(auditing);
			/*******添加审核结果成功---记录日志*******/
			if(status.equals(OperatorsStatus.WAIT_AUDITING)){//初审
				baseDao.addSyslog(SysLogType.AGENT_AUDITING.getCode(), SysLogType.AGENT_AUDITING.getName(), accountNo, "操作员"+accountNo+"【"+auditing.getAudit_name()+"】代理商【"+auditing.getObj_Audited()+"】成功");
			}else{
				baseDao.addSyslog(SysLogType.AGENT_RE_AUDITING.getCode(), SysLogType.AGENT_RE_AUDITING.getName(), accountNo, "操作员"+accountNo+"【"+auditing.getAudit_name()+"】代理商【"+auditing.getObj_Audited()+"】成功");
			}
			
			
			//查询代理商基本信息
			Operators operators=agentInfoDao.queryOperatorInfo(auditing.getObj_Audited());
			
			operators.setStatus(status);  
			operators.setDtLostmod(new Date());
			String password = Utils.getpass(10);
			operators.setPwd(MD5Utils.MD5Encode(password));
			Date cDate = new Date();
			String cStr = DateFormatUtils.format(cDate, "yyyyMMdd");
			operators.setUpdatePWD(cStr);
			agentInfoDao.editUpdate(operators);
			
			//把密码发送给客户。
			MessageSend messSend = new MessageSend();
			messSend.setPhoneNumber(operators.getMobile());
			messSend.setMesContent("尊敬的用户：您好，您在我司pos运营管理平台的账号是："+operators.getAccountNo()+",密码为："+password+",访问地址为：http://140.207.82.206:8090/fmposm");
			messSend.setSendFlag("0");
			Date date = new Date();
			messSend.setDtCreate(date);
			messSend.setDtLastmod(date);
			
			baseDao.editSave(messSend);
			
			
			/*******修改代理商状态成功--------记录日志*******/
			if(status.equals(OperatorsStatus.WAIT_AUDITING)){//初审
				baseDao.addSyslog(SysLogType.AGENT_AUDITING.getCode(), SysLogType.AGENT_AUDITING.getName(), accountNo, "操作员"+accountNo+"修改代理商【"+auditing.getObj_Audited()+"】状态成功,修改为【"+OperatorsStatus.valueOfCode(status).getName()+"】");
			}else{
				baseDao.addSyslog(SysLogType.AGENT_RE_AUDITING.getCode(), SysLogType.AGENT_RE_AUDITING.getName(), accountNo, "操作员"+accountNo+"修改代理商【"+auditing.getObj_Audited()+"】状态成功,修改为【"+OperatorsStatus.valueOfCode(status).getName()+"】");
			}
			result=true;
		} catch (DaoException e) {
			logger.debug(e.getMessage(), e);
			throw new ServiceException("代理商初审失败");
		}
		
		return result;
	}

}
